<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" xmlns:func="http://exslt.org/functions" xmlns:str="http://exslt.org/strings" extension-element-prefixes="date func str" version="1.0">

    <xsl:param name="uri_root" />
    <xsl:param name="uri_path" />
    <xsl:param name="publish_uri" />
    <xsl:param name="publish_name" />
    <xsl:param name="author_name" />
    <xsl:param name="author_email" />
    <xsl:variable name="uri">
	<xsl:value-of select="$uri_root" /><xsl:value-of select="$uri_path" /> 
    </xsl:variable>

    <xsl:output omit-xml-declaration="no" indent="yes" encoding="UTF-8" method="xml" />       
    
    <xsl:template match="log">
      <feed xmlns="http://www.w3.org/2005/Atom">
        <id><xsl:value-of select="$uri" /></id>
        <title type="text"><xsl:value-of select="$publish_name" /></title>
        <author>
          <!-- need to parameterize -->
          <name><xsl:value-of select="$author_name" /></name>
          <email><xsl:value-of select="$author_email" /></email>
        </author>
        <updated><xsl:value-of select="logentry/date[1]"/></updated>
        <xsl:if test="$publish_uri"><link rel="self" href="{$publish_uri}" /></xsl:if>
        <xsl:apply-templates select="logentry" />
      </feed>
    </xsl:template>
    
    <xsl:template match="logentry">
      <entry xmlns="http://www.w3.org/2005/Atom">
        <id><xsl:value-of select="$uri"/>/<xsl:value-of select="@revision"/></id>
        <link href="{$uri}#{@revision}" />
        <author>
          <name><xsl:value-of select="author"/></name>
          <email><xsl:value-of select="author"/></email>
        </author>
        <updated><xsl:value-of select="date"/></updated>
        <published><xsl:value-of select="date"/></published>
        <title type="text">
           #<xsl:value-of select="@revision"/>
           <xsl:value-of select="substring(substring-after(msg,':'),0,100)"/>
        </title>
        <content type="xhtml">
          <div xmlns="http://www.w3.org/1999/xhtml">
            <p><xsl:value-of select="msg"/></p>
            <xsl:call-template name="affectedpaths">
              <xsl:with-param name="mode">A</xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="affectedpaths">
              <xsl:with-param name="mode">D</xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="affectedpaths">
              <xsl:with-param name="mode">M</xsl:with-param>
            </xsl:call-template>
            <xsl:call-template name="affectedpaths">
              <xsl:with-param name="mode">R</xsl:with-param>
            </xsl:call-template>
          </div>
        </content>
      </entry>
    </xsl:template>

    <xsl:template name="affectedpaths">
      <xsl:param name="mode" />
      <xsl:if test="paths/path[@action=$mode]">
      <dl xmlns="http://www.w3.org/1999/xhtml">
        <dt><xsl:call-template name="svncommand"><xsl:with-param name="command" select="$mode" /></xsl:call-template></dt>
        <dd>
          <ul>
            <xsl:for-each select="paths/path[@action=$mode]">
            <li>
              <a href="{$uri_root}{current()}"><xsl:value-of select="current()" /></a>
              <xsl:if test="@copyfrom-path">
              (from <a href="{$uri_root}{@copyfrom-path}"><xsl:value-of select="@copyfrom-path" /></a> revision <xsl:value-of select="@copyfrom-rev" />)
              </xsl:if>
            </li>
            </xsl:for-each>
          </ul>
        </dd>
      </dl>
      </xsl:if>
    </xsl:template>

    <xsl:template name="svncommand">
      <xsl:param name="command"/>
      <xsl:choose>
        <xsl:when test="$command = 'A'">Added:</xsl:when>
        <xsl:when test="$command = 'D'">Deleted:</xsl:when>
        <xsl:when test="$command = 'M'">Modified:</xsl:when>
        <xsl:when test="$command = 'R'">Replaced:</xsl:when>
      </xsl:choose>
    </xsl:template>

</xsl:stylesheet>